﻿Imports System.Data.SqlClient
Public Class Frm_mohamin_mowakil
    Dim deClt As DictionaryEntry
    Dim dossAvo As Dossier_Avocat_Partenaire
    'Remplir le comboBox par le nom et le prenom avec un id
    Public Sub remplirMo7ami()
        Partager.moi.remplir_List_avoca_partenaire()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Avocat_partenaire In Partager.moi.List_avoca_partenaire
            str = clt.Nom_a & "  " & clt.Prenom_a
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            Comb_nom.Items.Add(ImageType)
        Next
        Comb_nom.DisplayMember = "value"
        Comb_nom.ValueMember = "key"
        If Comb_nom.Items.Count > 0 Then
            Comb_nom.SelectedIndex = 0
        End If
    End Sub


    Public Sub remplirLismohami()
        List_tous.Items.Clear()
        Dim ListViewItem1 As System.Windows.Forms.ListViewItem
        Dim Str As String = ""
        For Each dossAvo As Dossier_Avocat_Partenaire In Partager.dv.List_avoca_partenaire
            For Each avo As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                If dossAvo.Id_Avocat_Part = avo.Id Then

                    ListViewItem1 = New System.Windows.Forms.ListViewItem(New String() {"", avo.Num_Prof, avo.Nom_a, avo.Prenom_a, avo.Id}, -1)
                    'il est indispensable d'ajouter chekHand false et true
                    Partager.checkHand = False
                    If dossAvo.nomme Then
                        ListViewItem1.Checked = True
                    Else
                        ListViewItem1.Checked = False
                    End If
                    Me.List_tous.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1})
                    Partager.checkHand = True
                End If
            Next
        Next
    End Sub


    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        Me.Enabled = False
        Partager.type_rech_mowakil = "Frm_mohamin_mowakil"
        Frm_Rech_Avoc_Clien.Show()
    End Sub

    Private Sub Frm_mohamin_mowakil_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Idafa_Mowakilin.Enabled = True
    End Sub

    Private Sub Frm_mohamin_mowakil_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()

        Try
            txt_marji3ona.Text = Partager.dv.Num
            '----------------------------------------------------------------
            remplirMo7ami()
            Partager.dv.remplir_List_avoca_partenaire()
            remplirLismohami()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If Comb_nom.Items.Count > 0 Then
            Comb_nom.SelectedIndex = 0
        End If
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            If Comb_nom.Text <> "" Then

                If MsgBox("هل تريد فعلا إضافة هذا المحامي لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                    Partager.close()
                    deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                    Dim idClt As Integer = CInt(deClt.Key)
                    dossAvo = New Dossier_Avocat_Partenaire(Partager.dv.id, idClt, "1")
                    Partager.dv.Ajouter_avoca_partenaire(dossAvo)

                    remplirLismohami()
                    MsgBox("تمت إضافة المحامي للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                End If

            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("PK_Dossiers_Avocat_Partonaire_1") Then
                MsgBox("عفوا، هذا المحامي تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            dossAvo = Partager.dv.List_avoca_partenaire(List_tous.SelectedIndices.Item(0))
            Partager.dv.suprimmer_avoca_partenaire(dossAvo.Id_Avocat_Part)
            Partager.dv.remplir_List_avoca_partenaire()
            remplirLismohami()
            MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
        Else
            MsgBox("اختر محامي من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub


    Private Sub List_tous_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_tous.ItemChecked

        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_tous.Items
                dossAvo = New Dossier_Avocat_Partenaire
                dossAvo = Partager.dv.List_avoca_partenaire(i)
                dossAvo.nomme = item1.Checked
                Partager.dv.modifierAvoPartNomme(dossAvo)
                i += 1
            Next
        End If
    End Sub

End Class